<?php
namespace BeautyParlorAdmin\Controller;

use Think\Controller;
use Common\Util\UploadHandler;

class BeauticianController extends BaseController {
    public function index(){
    	$Model = D('Beautician');
    	
    	$id = $this->id;
    	
    	$list = $Model->field(array('b.*'))
    			->join('b left join '.C('DB_PREFIX').'beauty_parlor_employe as emp on b.id=emp.beautician_id')
    			->where(array('emp.bp_id' => $id))->select();
    	
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function add() {
    	if (!IS_POST) {
    		$this->assign('number', md5(microtime()));
    		$this->assign('jobTitles', M('job_title')->select());
    		$this->display();
    	} else {
    		$Model = D('Beautician');
    		if ($Model->addBeautician(I('post.'))) {
    			$this->success('添加成功');
    		} else {
    			$this->error($Model->getError());
    		}
    	}
    }
    
    //获取经纬度
	public function getCoordinates(){
		$arrData=CheckCoordinate();
		$this->ajaxReturn($arrData);
	}
	
    public function edit() {
    	
//     	dump(M()->query('alter table mdd_beautician change lotitude latitude varchar(30) not Null;'));
//     	dump(M()->query('alter table mdd_beautician add latitude varchar(30) not Null;'));
//     	dump(M()->query('SHOW FULL COLUMNS FROM mdd_beautician'));
//     	die;
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$Model = D('Beautician');
    		
    		$item = $Model->getBeauticianInfo($id);
    		$photos = D('Photo')->getTargetPhotos($id);
    		
    		$this->assign('jobTitles', M('job_title')->select());
    		if ($photos) {
    			$this->assign('photos', $photos);
    			$this->assign('number', $photos [0] ['upload_number']);
    		} else {
    			$this->assign('number', md5(microtime()));
    		}
    		
    		$experiences = D('WorkExperience')->getTargetExperiences($id);
    		$this->assign('experiences', $experiences);
    		
    		$this->assign('item', $item);
    		$this->display();
    	} else {
    		$Model = D('Beautician');
    		if ($Model->updateInfo(I('post.'))) {
    			$this->success('修改成功', U('/Admin/Beautician/index'));
    		} else {
    			$this->error($Model->getError());
    		}
    	}
    }
    
    public function jobTitleAdmin() {
    	$JobTitleModel = M('job_title');
    	
    	$this->assign('list', $JobTitleModel->where(array('status' => 1))->select());
    	$this->display();
    }
    
    public function addJobTitle() {
    	if (!IS_POST) {
    		$this->display();
    	} else {
    		$JobTitleModel = M('job_title');
    		
    		$data = array(
    			'title' => I('title'),
    			'created' => time()
    		);
    		$resutl = $JobTitleModel->add($data);
    		if ($resutl) {
    			$this->success('添加职称成功');
    		} else {
    			$this->error('添加职称失败');
    		}
    	}
    }
    
    public function editJobTitle() {
    	$JobTitleModel = M('job_title');
    	
    	if (!IS_POST) {
    		$id = I('get.id');
    		
    		$item = $JobTitleModel->where(array('id' => $id))->find();
    		$this->assign('item', $item);
    		$this->display();
    	} else {
    		$id = I('post.id');
    		$data = array(
    			'title' => I('title'),
    		);
    		$resutl = $JobTitleModel->where(array('id' => $id))->save($data);
    		if ($resutl) {
    			$this->success('修改职称成功');
    		} else {
    			$this->error('修改职称失败');
    		}
    	}
    }
    
    public function delete() {
    	if(IS_GET){
    		$Id=$_GET['id'];    
    		$BeauModel=M('Beautician');
    		$Records=$BeauModel->where("id='$Id'")->delete(); // 删除id为5的用户数据
    		if($Records>0){
    			$this->success('删除用户成功',U('index'));
    		}else {
    			$this->error('删除用户失败');
    		}
    		
    	}	
    }
    
    public function uploadProfileImg() {
    	$options = C('PROFILE_FILE_UPLODE_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Profile/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	
    	$this->ajaxReturn($result [$param_name] [0]);
    }
    
    public function uploadCertification() {
    	$num = I('post.num');
    	$options = C('CERTIFICATION_FILE_UPLODE_CONFIG');
    	$options ['upload_dir'] .= $num.'/';
    	$options ['upload_url'] = get_full_url().'/Uploads/Certification/'.$num.'/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name]);
    }
}